#include <iostream>
#include <vector>
#include<windows.h>
using namespace std;

// 迭代器接口
class Iterator
{
    public:
        virtual bool hasNext() = 0;
        virtual string next() = 0;
        virtual ~Iterator() = default;
};

// 具体迭代器
class BookIterator : public Iterator
{
private:
    vector<string> &books; // 引用集合
    int index;

public:
    BookIterator(vector<string> &books) : books(books), index(0) {}

    bool hasNext() override
    {
        return index < books.size();
    }

    string next() override
    {
        return books[index++];
    }
};

// 集合接口
class Aggregate
{
public:
    virtual Iterator *createIterator() = 0; // 创建迭代器
    virtual ~Aggregate() = default;
};

// 具体集合类
class BookCollection : public Aggregate
{
private:
    vector<string> books;

public:
    void addBook(const string &book)
    {
        books.push_back(book);
    }

    Iterator *createIterator() override
    {
        return new BookIterator(books);
    }
};

int main()
{

    SetConsoleOutputCP(65001);
    // 创建集合并添加书籍
    BookCollection collection;
    collection.addBook("The Catcher in the Rye");
    collection.addBook("To Kill a Mockingbird");
    collection.addBook("1984");

    // 使用迭代器遍历集合中的元素
    Iterator *iterator = collection.createIterator();

    while (iterator->hasNext())
    {
        cout << iterator->next() << endl;
    }

    delete iterator; // 不要忘记清理
    return 0;
}